
package net.phoenix.repository.nodetype;

import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;

import javax.jcr.RepositoryException;
import javax.jcr.UnsupportedRepositoryOperationException;
import javax.jcr.nodetype.NoSuchNodeTypeException;
import javax.jcr.nodetype.NodeDefinitionTemplate;
import javax.jcr.nodetype.NodeType;
import javax.jcr.nodetype.NodeTypeDefinition;
import javax.jcr.nodetype.NodeTypeIterator;
import javax.jcr.nodetype.NodeTypeManager;
import javax.jcr.nodetype.NodeTypeTemplate;
import javax.jcr.nodetype.PropertyDefinitionTemplate;

/**
 * 
 * @author lixf
 *
 */
public abstract class GeneralNodeTypeManager implements NodeTypeManager {
	private Map<String, NodeType> types;

	public GeneralNodeTypeManager() {
		this.types = Collections.synchronizedMap(new LinkedHashMap<String, NodeType>());
	}

	/**
	 * @param type
	 */
	protected void addNodeType(NodeType type) {
		this.types.put(type.getName(), type);
	}

	/**
	 * @param type
	 */
	protected void remove(NodeType type) {
		this.types.remove(type.getName());
	}
	/* (non-Javadoc)
	 * @see javax.jcr.nodetype.NodeTypeManager#unregisterNodeType(java.lang.String)
	 */
	@Override
	public void unregisterNodeType(String name) throws UnsupportedRepositoryOperationException, NoSuchNodeTypeException, RepositoryException {
		this.types.remove(name);
	}

	/* (non-Javadoc)
	 * @see javax.jcr.nodetype.NodeTypeManager#unregisterNodeTypes(java.lang.String[])
	 */
	@Override
	public void unregisterNodeTypes(String[] names) throws UnsupportedRepositoryOperationException, NoSuchNodeTypeException, RepositoryException {
		for(int i=0;i<names.length;i++)
			this.types.remove(names[i]);
	}
	/* (non-Javadoc)
	 * @see javax.jcr.nodetype.NodeTypeManager#getAllNodeTypes()
	 */
	@Override
	public NodeTypeIterator getAllNodeTypes() {
		return new BasicNodeTypeIterator(this.types.values());
	}

	/* (non-Javadoc)
	 * @see javax.jcr.nodetype.NodeTypeManager#getNodeType(java.lang.String)
	 */
	@Override
	public NodeType getNodeType(String pkid) throws NoSuchNodeTypeException{
		if(!this.types.containsKey(pkid))
			throw new NoSuchNodeTypeException(pkid);
		return this.types.get(pkid);
	}

	
	
	/* (non-Javadoc)
	 * @see javax.jcr.nodetype.NodeTypeManager#hasNodeType(java.lang.String)
	 */
	@Override
	public boolean hasNodeType(String name) throws RepositoryException {
			return this.types.containsKey(name);
	}

	/* (non-Javadoc)
	 * @see javax.jcr.nodetype.NodeTypeManager#getPrimaryNodeTypes()
	 */
	@Override
	public NodeTypeIterator getPrimaryNodeTypes() throws RepositoryException {
		return this.getAllNodeTypes();
	}

	/* (non-Javadoc)
	 * @see javax.jcr.nodetype.NodeTypeManager#getMixinNodeTypes()
	 */
	@Override
	public NodeTypeIterator getMixinNodeTypes() throws RepositoryException {
		return this.getAllNodeTypes();
	}

	/* (non-Javadoc)
	 * @see javax.jcr.nodetype.NodeTypeManager#createNodeTypeTemplate()
	 */
	@Override
	public NodeTypeTemplate createNodeTypeTemplate()
			throws UnsupportedRepositoryOperationException, RepositoryException {
		return null;
	}

	@Override
	public NodeTypeTemplate createNodeTypeTemplate(NodeTypeDefinition ntd)
			throws UnsupportedRepositoryOperationException, RepositoryException {
		return null;
	}

	@Override
	public NodeDefinitionTemplate createNodeDefinitionTemplate()
			throws UnsupportedRepositoryOperationException, RepositoryException {
		return null;
	}

	@Override
	public PropertyDefinitionTemplate createPropertyDefinitionTemplate()
			throws UnsupportedRepositoryOperationException, RepositoryException {
		return null;
	}


}
